package com.publicize.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.publicize.commom.servicebase.exception.MyException;
import com.publicize.entity.Examination;
import com.publicize.entity.ExaminationDetail;
import com.publicize.entity.Vo.ExaminationVo;
import com.publicize.entity.Vo.QueryVo;
import com.publicize.entity.Vo.option;
import com.publicize.listener.DemoListener;
import com.publicize.mapper.ExaminationMapper;
import com.publicize.service.ExaminationDetailService;
import com.publicize.service.ExaminationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 知识竞赛测试表 服务实现类
 * </p>
 *
 * @author bs
 * @since 2021-03-05
 */
@Service
public class ExaminationServiceImpl extends ServiceImpl<ExaminationMapper, Examination> implements ExaminationService {
@Resource
private ExaminationDetailService examinationDetailService;
@Resource
private ExaminationService examinationService;
    @Override
    public boolean remove(String number) {
        //删除试题
        int delete = baseMapper.deleteById(number);
        if(delete!=1) throw new MyException("删除失败",201);
        //根据试题id搜索对应题目
        QueryWrapper<ExaminationDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("examination_number",number);
        //根据条件搜索对应题目
        List<ExaminationDetail> list = examinationDetailService.list(queryWrapper);
        //存储id
        for (int i = 0; i <list.size() ; i++) {
            list.get(i).setIsDeleted(1);
        }
        boolean deleteIds = examinationDetailService.updateBatchById(list);
        return deleteIds;
    }
//增加试题
    @Override
    public Examination insertOrUpdate(Examination examination) {
        //判断是增加还是插入
        //1 根据编号查询表中是否有数据
        Examination examinationlast = baseMapper.selectById(examination.getNumber());
        //查询不到,插入操作
        if(examinationlast==null){
            int insert = baseMapper.insert(examination);
            if(insert!=1) throw new MyException("插入失败",201);
        }else{//编辑操作
            int update = baseMapper.updateById(examination);
            if(update!=1) throw new MyException("编辑失败",201);
        }
        return examination;
    }
    //取消上传excel
    @Override
    public boolean unUpload(String number) {
        //根据编号查找到对应的试题
        QueryWrapper<ExaminationDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("examination_number",number);
        //删除试题题目
        boolean remove = examinationDetailService.remove(queryWrapper);
        //删除试题
        examinationService.removeById(number);
        return remove;
    }

    @Override
    public int getExaminationCount(QueryVo queryVo) {
        return baseMapper.selectExaminationCount(queryVo);
    }

    @Override
    public List<ExaminationVo> getExaminationList(QueryVo queryVo) {
        return baseMapper.selectExaminationList(queryVo);
    }

    @Override
    public List<ExaminationVo> getAll() {
        return baseMapper.getAll();
    }

    //上传试题
    @Override
    public Map<String, String> save(String examinationNumber, MultipartFile file) {
        QueryWrapper<ExaminationDetail> queryWrapper=new QueryWrapper<>();
        //对文件进行解析并返回题目资料
        List<option> options = returnOptions(file);
        //存储试题详细资料
        List<ExaminationDetail> examinationDetails = new ArrayList<>();
        //遍历返回的option试题资料并copy到examinationDetail中
        for (int i = 0; i < options.size(); i++){
            ExaminationDetail examinationDetail=new ExaminationDetail();
            examinationDetail.setExaminationNumber(examinationNumber);
            BeanUtils.copyProperties(options.get(i), examinationDetail);
            examinationDetails.add(examinationDetail);
    }
        //批量插入数据
        boolean saveBatch= examinationDetailService.saveBatch(examinationDetails);
        if(!saveBatch) throw new MyException("插入试题失败",201);
        Map<String,String> map=new HashMap<>();
        map.put("topicNum", String.valueOf(options.size()));
        map.put("examinationNumber",examinationNumber);
        options.clear();
        return map;
    }
    public static List<option> returnOptions(MultipartFile file){
        InputStream inputStream = null;
        try {
            //获取文件输出流
            inputStream = file.getInputStream();
            InputStream in = new BufferedInputStream(inputStream);
            //读取文件解析
            ExcelReader excelReader = EasyExcel.read(in, option.class, new DemoListener()).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
            //返回题目资料
            List<option> options = DemoListener.returnOptions();
            // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
            excelReader.finish();
            return options;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
